<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
        <el-form-item label="合作商名称" prop="partnerName">
          <el-input
            v-model="queryParams.partnerName"
            placeholder="请输入合作商名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="合作商电话" prop="partnerPhone">
          <el-input
            v-model="queryParams.partnerPhone"
            placeholder="请输入合作商电话"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['partner:partner:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['partner:partner:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['partner:partner:remove']"
          >删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="partnerList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" />
        <el-table-column label="合作商名称" align="center" prop="partnerName" />
        <el-table-column label="合作商电话" align="center" prop="partnerPhone" />
        <el-table-column label="银行账户名称" align="center" prop="partnerBankAccount" />
        <el-table-column label="银行名称" align="center" prop="partnerBankName" />
        <el-table-column label="银行卡号" align="center" prop="partnerBankNo" />
        <el-table-column label="开户行" align="center" prop="partnerBankAddress" />
        <!-- <el-table-column label="支付宝小程序用户" align="center" prop="partnerZfbMiniUserName" /> -->
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding" width="220">
          <template slot-scope="scope">
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-user"
              @click="handleBindUser(scope.row)"
            >绑定小程序用户</el-button> -->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-user"
              @click="zfbQrcode(scope.row)"
            >支付宝小程序二维码</el-button> 
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['partner:partner:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['partner:partner:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <el-dialog title="绑定支付宝小程序用户" :visible.sync="openZfbQrCode" width="350px" append-to-body>
        <div v-if="qrCodeUrl">
          <img :src="qrCodeUrl" alt="QR Code" />
        </div>
      </el-dialog>

      <el-dialog title="绑定支付宝小程序用户" :visible.sync="openZfb" width="800px" append-to-body>
        <el-form ref="userFormRef" :model="userForm" label-width="180px">
          <el-form-item label="支付宝小程序用户" prop="partnerName">
              <el-select v-model="userForm.userId" remote  :remote-method="remoteMethod" placeholder="请选择用户" :loading="loading" @focus="remoteMethod">
                <el-option v-for="user in users" :key="user.id" :label="user.nickName + ' : '+ user.userName" :value="user.id" ></el-option>
                <el-pagination  @current-change="loadMore" :current-page="userCurrentPage" :page-size="userPageSize" layout="prev, pager, next" :total="userTotal" ></el-pagination>
              </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitUserForm">确 定</el-button>
          <el-button @click="cancelUserForm">取 消</el-button>
        </div>
      </el-dialog>
  
      <!-- 添加或修改合作商管理对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="合作商名称" prop="partnerName">
            <el-input v-model="form.partnerName" placeholder="请输入合作商名称" />
          </el-form-item>
          <el-form-item label="合作商电话" prop="partnerPhone">
            <el-input v-model="form.partnerPhone" placeholder="请输入合作商电话" />
          </el-form-item>
          <el-form-item label="银行账户名称" prop="partnerBankAccount">
            <el-input v-model="form.partnerBankAccount" placeholder="请输入合作商银行账户名称" />
          </el-form-item>
          <el-form-item label="银行名称" prop="partnerBankName">
            <el-input v-model="form.partnerBankName" placeholder="请输入合作商银行名称" />
          </el-form-item>
          <el-form-item label="银行卡号" prop="partnerBankNo">
            <el-input v-model="form.partnerBankNo" placeholder="请输入合作商银行卡号" />
          </el-form-item>
          <el-form-item label="开户行" prop="partnerBankAddress">
            <el-input v-model="form.partnerBankAddress" placeholder="请输入合作商开户行" />
          </el-form-item>
          <!-- <el-form-item label="合作商支付宝小程序用户id" prop="partnerZfbMiniUserId">
            <el-input v-model="form.partnerZfbMiniUserId" placeholder="请输入合作商支付宝小程序用户id" />
          </el-form-item> -->
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listPartner, getPartner, delPartner, addPartner, updatePartner,queryZfbUser,bindzfbUser,generateQRCode } from "@/api/system/artPartner";
  
  export default {
    name: "Partner",
    data() {
      return {
        qrCodeUrl: null,
        selectedValue: '',
        users: [],
        userCurrentPage: 1,
        userPageSize: 10,
        userTotal: 0,
        openZfb: false,
        openZfbQrCode: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 合作商管理表格数据
        partnerList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          partnerName: null,
          partnerPhone: null,
          partnerBankAccount: null,
          partnerBankName: null,
          partnerBankNo: null,
          partnerBankAddress: null,
          partnerZfbMiniUserId: null,
        },
        query: {
          pageNum: 1,
          pageSize: 10,
          nickName: null,
          userName: null,
        },
        // 表单参数
        form: {},
        userForm: {
          id: null,
          nickName: null,
          userName: null,
        },
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      zfbQrcode(row) {
        this.openZfbQrCode = true;        
        this.qrCodeUrl = null;
        generateQRCode(row.id).then(res => {
            this.qrCodeUrl = res;
        })
      },
      submitUserForm() {
        console.log(this.userForm);
        bindzfbUser(this.userForm).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.openZfb = false;
          this.getList();
        });
      },
      cancelUserForm() {
        this.openZfb = false;
      },
      remoteMethod() {
        this.loading = true;
        console.log(this.userForm.nickName);
        if(this.userForm.nickName) {
          this.query.nickName = this.userForm.nickName;
        }
        queryZfbUser(this.query).then(response => {
          this.users = response.rows;
          this.userTotal = response.total;
          this.loading = false;
        });
      // axios.get(`/users?page=${this.currentPage}&size=${this.pageSize}`)
      //   .then(response => {
      //     this.users = response.data.data;
      //     this.total = response.data.total;
      //     this.loading = false;
      //   })
      //   .catch(error => {
      //     console.error(error);
      //     this.loading = false;
      //   });
    },
      loadMore(newPage) {
        this.query.pageNum = newPage;
        this.remoteMethod();
      },
      //查询小程序用户
      handleBindUser(row) {
        this.openZfb = true;
        this.userForm = {
          id: null,
          nickName: null,
          userName: null,
        };
        this.userForm.id = row.id;
      },
      /** 查询合作商管理列表 */
      getList() {
        this.loading = true;
        listPartner(this.queryParams).then(response => {
          this.partnerList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          partnerName: null,
          partnerPhone: null,
          partnerBankAccount: null,
          partnerBankName: null,
          partnerBankNo: null,
          partnerBankAddress: null,
          partnerZfbMiniUserId: null,
          remark: null,
          createTime: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加合作商管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getPartner(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改合作商管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updatePartner(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPartner(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除合作商管理编号为"' + ids + '"的数据项？').then(function() {
          return delPartner(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('partner/partner/export', {
          ...this.queryParams
        }, `partner_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  